import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerBusinessMainApi } from '#/api/per/businessMain';

import { DICT_TYPE } from '@vben/constants';
import { handleTree } from '@vben/utils';

import { getOperateCategoriesList } from '#/api/system/operateCategories';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'operateCategoryId',
      label: '业务分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getOperateCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择业务分类分类',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'operateCategoryCode',
      label: '业务分类编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入业务分类编码',
      },
    },
    {
      fieldName: 'operateCategoryName',
      label: '业务分类名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入业务分类名称',
      },
    },
    {
      fieldName: 'objectType',
      label: '业务对象类型',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入业务对象类型',
      },
    },
    {
      fieldName: 'objectName',
      label: '业务对象名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入业务对象名称',
      },
    },
    {
      fieldName: 'naturalYear',
      label: '年度',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerBusinessMainApi.BusinessMain>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'naturalYear',
      title: '年度',
      minWidth: 120,
    },
    {
      field: 'id',
      title: '业务主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'operateCategoryCode',
      title: '业务分类编码',
      minWidth: 120,
    },
    {
      field: 'operateCategoryName',
      title: '业务分类名称',
      minWidth: 120,
    },
    {
      field: 'objectType',
      title: '业务对象类型',
      minWidth: 120,
    },
    {
      field: 'objectName',
      title: '业务对象名称',
      minWidth: 120,
    },

    {
      field: 'businessStatus',
      title: '业务状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_AUDIT_STATUS },
      },
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

// 审核意见
export function useAuditFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'auditComment',
      label: '审核意见',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入审核意见',
      },
    },
  ];
}
